package com.hexiang.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;
import java.io.IOException;

@Component
public class ElasticSearchConfig {

    @Value("${spring.elasticsearch.host}")
    private String esHost;

    @Value("${spring.elasticsearch.port}")
    private int esPort;

    private RestHighLevelClient restHighLevelClient;

    @Bean
    public RestHighLevelClient restClient() {
        restHighLevelClient = new RestHighLevelClient(RestClient.builder(new HttpHost(esHost, esPort)));
        return restHighLevelClient;
    }

    @PreDestroy
    public void close() throws IOException {
        restHighLevelClient.close();
    }
}